package org.upc.edu.AIAExamples.communication;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import jade.lang.acl.ACLMessage;

public class Rellotge extends Agent{

	Integer n;
	
	public class Ticker extends TickerBehaviour
    {
        public Ticker(Agent a, long period)
        {
            super(a,period);            
        }

         public void onStart()
        {
        }

        public int onEnd()
        {
        	return 0;
        }

        public void onTick()
        {
        	System.out.println();
        	AID central = new AID();
            central.setLocalName("central");
            ACLMessage tick = new ACLMessage();
        	tick.addReceiver(central);
            for (int i = 1; i <= n ; ++i){
            	AID casai = new AID();
                casai.setLocalName("casa"+i);        	
    			tick.addReceiver(casai);
            }
            
			tick.setSender(myAgent.getAID());
			myAgent.send(tick);
			System.out.println(myAgent.getLocalName()+ " :TICK");
        }

    }

    protected void setup()
    {
     Ticker b = new Ticker(this, 4000);
     Object[] args = getArguments();
     n = Integer.parseInt(args[0].toString());
     this.addBehaviour(b);
    }

}